package com.course.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2 // 开启swagger
@EnableKnife4j  // 开启knife4j
public class SwaggerConfiguration {
    /**
     * 需要往 Spring IOC容器中 注册Swagger
     * Docket 为Swagger提供的类 用于声明文档的类型
     * @return
     */
    @Bean
    public Docket docket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2);    // 指定使用Swagger2
        docket = docket
                .apiInfo(apiInfo()) // 文档的详细信息
                .select()
//                 指定当前项目需要扫描的包 指定接口控制器层的包路径
                .apis(RequestHandlerSelectors.basePackage("com.course.controller"))
                // 为指定规则的API生成接口文档  any() 表示全部
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * API Info 用于配置文档的详细信息
     * 借助工厂类 APIInfoBuilder构建
     * @return
     */
    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("课程服务")
                .description("课程服务控制层")
                .version("1.0.0")
                .build();
    }
}
